package com.neusoft.hifly.information.article.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.neusoft.hifly.commons.lang.LongUtil;
import com.neusoft.hifly.core.basic.service.impl.CTSaaSBaseServiceImpl;
import com.neusoft.hifly.core.token.TokenContextHolder;
import com.neusoft.hifly.dynamicds.multi.annotation.MultiDynamicRoutingDataSource;
import com.neusoft.hifly.information.article.dao.IArticleLogDao;
import com.neusoft.hifly.information.article.entity.ArticleLogObj;
import com.neusoft.hifly.information.article.service.IArticleLogService;

/**
 * Title: 文章资讯服务
 * <p>
 * Description: 文章审核日志 Service
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service
@MultiDynamicRoutingDataSource
public class ArticleLogServiceImpl extends CTSaaSBaseServiceImpl implements IArticleLogService {
	@Autowired(required = false)
	private IArticleLogDao articleLogDao;

	/**
	 * 保存对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param obj
	 *            对象
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
	public void save(final String organCode, final long organLogicId, final ArticleLogObj obj) {
		if (StringUtils.isEmpty(organCode) || obj == null) {
			return;
		}
		if (StringUtils.isEmpty(obj.getOrganCode())) {
			obj.setOrganCode(organCode);
		}
		if (LongUtil.isEmpty(obj.getOrganLogicId())) {
			obj.setOrganLogicId(organLogicId);
		}

		if (LongUtil.isEmpty(obj.getOrganLogicId())) {
			obj.setOrganLogicId(TokenContextHolder.get().getOrganLogicId());
		}

		obj.setOrganLogicId(organLogicId);

		obj.setId(idWorker.nextId());

		if (LongUtil.isEmpty(obj.getLogicId())) {
			obj.setLogicId(idWorker.nextId());
		}
		obj.setOperatorTime(LocalDateTime.now());

		if (StringUtils.isEmpty(obj.getOperatorName())) {
			obj.setOperatorName(TokenContextHolder.get().getUserName());
		}

		this.setForm(obj, obj);

		articleLogDao.insert(obj);

	}

	/**
	 * 根据id查询对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param logicId
	 *            对象logicId
	 * @return 对象
	 */
	@Override
	public ArticleLogObj getByLogicId(final String organCode, final long organLogicId, final long logicId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return articleLogDao.getByLogicId(organCode, organLogicId, logicId);
	}

	/**
	 * 查询文章日志
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param articleId
	 *            文章ID
	 * @return 对象集合
	 */
	@Override
	public List<ArticleLogObj> findByArticleId(final String organCode, final long organLogicId, final long articleId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return articleLogDao.findByArticleId(organCode, organLogicId, articleId);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 集合
	 */
	@Override
	public List<ArticleLogObj> search(final String organCode, final long organLogicId, Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final QueryWrapper<ArticleLogObj> queryWrapper = new QueryWrapper<ArticleLogObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, "UPDATE_TIME");
		return articleLogDao.selectList(queryWrapper);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param offset
	 *            当前页数
	 * @param limit
	 *            分页长度
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 返回Map
	 */
	@Override
	public IPage<ArticleLogObj> search(final String organCode, final long organLogicId, final int offset,
			final int limit, Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final IPage<ArticleLogObj> page = new Page<ArticleLogObj>(offset, limit);
		final QueryWrapper<ArticleLogObj> queryWrapper = new QueryWrapper<ArticleLogObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, "UPDATE_TIME");
		return articleLogDao.selectPage(page, queryWrapper);
	}

	/**
	 * 复制信息
	 *
	 * @param fromObj
	 *            原信息
	 * @param toObj
	 *            新信息
	 */
	private void setForm(final ArticleLogObj fromObj, final ArticleLogObj toObj) {
		toObj.setOrganLogicId(fromObj.getOrganLogicId());
		toObj.setArticleId(fromObj.getArticleId());
		toObj.setOperatorStep(StringUtils.trimToEmpty(fromObj.getOperatorStep()));
		toObj.setOperatorId(fromObj.getOperatorId());
		toObj.setOperatorName(StringUtils.trimToEmpty(fromObj.getOperatorName()));
		toObj.setOperatorTime(fromObj.getOperatorTime());
		toObj.setOperatorContent(StringUtils.trimToEmpty(fromObj.getOperatorContent()));
		toObj.setExt1(StringUtils.trimToEmpty(fromObj.getExt1()));
		toObj.setExt2(StringUtils.trimToEmpty(fromObj.getExt2()));
		toObj.setExt3(StringUtils.trimToEmpty(fromObj.getExt3()));
		toObj.setExt4(StringUtils.trimToEmpty(fromObj.getExt4()));
		toObj.setExt5(StringUtils.trimToEmpty(fromObj.getExt5()));

	}

	/**
	 * 将对象属性替换成数据库列
	 *
	 * @param property
	 *            参数
	 */
	private void replaceProperty(final Map<String, Object> property) {
		if (property != null) {
			if (property.containsKey("organLogicId")) {
				property.put("ORGAN_LOGIC_ID", property.get("organLogicId"));
				property.remove("organLogicId");
			}
			if (property.containsKey("articleId")) {
				property.put("ARTICLE_ID", property.get("articleId"));
				property.remove("articleId");
			}
			if (property.containsKey("operatorStep")) {
				property.put("OPERATOR_STEP", property.get("operatorStep"));
				property.remove("operatorStep");
			}
			if (property.containsKey("operatorId")) {
				property.put("OPERATOR_ID", property.get("operatorId"));
				property.remove("operatorId");
			}
			if (property.containsKey("operatorName")) {
				property.put("OPERATOR_NAME", property.get("operatorName"));
				property.remove("operatorName");
			}
			if (property.containsKey("operatorTime")) {
				property.put("OPERATOR_TIME", property.get("operatorTime"));
				property.remove("operatorTime");
			}
			if (property.containsKey("operatorContent")) {
				property.put("OPERATOR_CONTENT", property.get("operatorContent"));
				property.remove("operatorContent");
			}
			if (property.containsKey("ext1")) {
				property.put("EXT1", property.get("ext1"));
				property.remove("ext1");
			}
			if (property.containsKey("ext2")) {
				property.put("EXT2", property.get("ext2"));
				property.remove("ext2");
			}
			if (property.containsKey("ext3")) {
				property.put("EXT3", property.get("ext3"));
				property.remove("ext3");
			}
			if (property.containsKey("ext4")) {
				property.put("EXT4", property.get("ext4"));
				property.remove("ext4");
			}
			if (property.containsKey("ext5")) {
				property.put("EXT5", property.get("ext5"));
				property.remove("ext5");
			}

		}
	}
}